package collections;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CollectionTest1 {
	public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException,
			InvocationTargetException {
		for (Method m : CollectionTest1.class.getDeclaredMethods()) {
			if (m.getName().contains("main")) {
				continue;
			}
			m.invoke(null);
		}
	}

	/**
	 * true,地址完全拷贝
	 */
	public static void test1() {
		System.out.println("=======test1=========");
		Object[] o = { new Object() };
		Object[] o2 = Arrays.copyOf(o, o.length);
		System.out.println(o[0] == o2[0]);
	}

	/**
	 * [java.lang.Object@ae3865e, null, null, java.lang.Object@5f8a8ae7]
	 * 集合元素个数1,就会把数据下标[1]这个元素设置成null,再后面的元素不变 即toArray(T[]
	 * a)方法只要a.length比迭代器的元素数量多,那么超过元素数量的第一个元素会被设置成null
	 */
	public static void test2() {
		Object[] o = { null, new Object(), null, new Object() };
		List<Object> list = Arrays.asList(new Object());
		list.toArray(o);
		System.out.println(Arrays.toString(o));
	}

	/**
	 * remove移除第一个equals的元素
	 */
	public static void test3() {
		System.out.println("=======test3=========");
		List<Object> list = Arrays.asList(null, null, null);
		ArrayList<Object> al = new ArrayList<Object>(list);
		al.remove(null);
		System.out.println(al);
	}

	/**
	 * 集合元素超过toArray(T[] a)的a.length,会返回新的数组
	 */
	public static void test4() {
		System.out.println("=======test4=========");
		Object[] o = { null, new Object(), null, new Object() };
		List<Object> list = new ArrayList<Object>(Arrays.asList(o));
		list.add(null);
		Object[] o2 = list.toArray(o);
		System.out.println(Arrays.toString(o));
		System.out.println(Arrays.toString(o2));
		System.out.println(o == o2);// false
		System.out.println(o[0] == o2[0]);// true
	}

	public static void test5() {
		System.out.println("=======test5=========");
		Object[] o = new Object[0];
		List<Object> o2 = Arrays.asList(o);
		System.out.println(o2.toArray() == o);// false,进行了数组拷贝
	}
}
